import csv
import os

import requests
from lxml import etree

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
}


def save_img(img_url, img_title):
    # 判断文件夹是否存在

    if not os.path.exists("../data/优信二手车"):
        # 没有就创建
        os.mkdir("../data/优信二手车")

    # 对img_title进行数据提取
    # 红旗 HS5 2019款 2.0T 自动 智联旗享版四驱价格
    infos = img_title.split(" ")

    brand = infos[0]
    moto = infos[1]
    cyear = infos[2]

    if not os.path.exists("../data/" + brand + "/image"):
        # 没有就创建
        os.makedirs("../data/" + brand + "/image")

    response = requests.get(url="http:"+img_url, headers=headers)

    with open("../data/" + brand + "/image" + "/" + brand + moto + cyear + ".jpg", "wb") as f:

        f.write(response.content)

    print("写入", img_title, "图片成功")


def save_info(info_title, price):
    # 判断文件夹是否存在

    if not os.path.exists("../data/优信二手车"):
        # 没有就创建
        os.mkdir("../data/优信二手车")

    # 对img_title进行数据提取
    # 红旗 HS5 2019款 2.0T 自动 智联旗享版四驱价格
    infos = info_title.split(" ")

    brand = infos[0]
    moto = infos[1]
    cyear = infos[2]

    if not os.path.exists("../data/" + brand):
        # 没有就创建
        os.makedirs("../data/" + brand)

    # 写入csv文件

    with open("../data/" + brand + "/" + moto + ".csv", "w+") as f:
        cf = csv.writer(f)
        cf.writerow([info_title, price, cyear])

    print("数据保存成功")


def main():
    for i in range(4, 6):
        print(i)
        url = "https://www.xin.com/suqian/i%s/" % i

        response = requests.get(url=url, headers=headers)

        xhtml = etree.HTML(response.content.decode())

        xpath_head = xhtml.xpath('//ul/li/div[2]')
        # title_list = xhtml.xpath('//ul/li/div[2]/a/div/h2/span/text()'
        for x in xpath_head:
            img_url = x.xpath('.//img/@src')[0]
            title = x.xpath('.//img/@alt')[0]
            price = x.xpath('.//p[1]/em/text()')[0].strip().replace(" ", "").replace("\n", "").replace("万", "")
            # 保存 图片
            save_img(img_url, title)
            # 保存信息
            save_info(title, price)


if __name__ == '__main__':
    main()
